package de.tabuma.pm.security.controller;

import de.tabuma.pm.*;
import de.tabuma.ssoclient.entity.*;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import de.tabuma.lib.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author alefen
 */
public class GroupPrivilegView extends tabumaview {

    private static final Logger log = Logger.getLogger(GroupPrivilegView.class.getName());

    public GroupPrivilegView(String servlet, Hashtable hash) {
        super(servlet, hash);
    }

    @Override
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        String message = "Change privileg settings in the table below.";
        String privileg = "privileg";
        String hasPrivileg = "has privileg?";
        String back = "back";
        String save = "save";
        String privilegSaved = "Change of privileges has been saved.";
        String privilegManagement = "Privileg Management";

        out.println("<div class=\"contentContent\">");
        out.println("<p class=\"contentContentHeader\">"+privilegManagement+"</p>");
        
        out.println("<p class=\"contentHeader\">"+message+"</p>");
        if(request.getAttribute(constants.privilegSaved)!=null) {
            out.println("<p class=\"contentHeader\">"+privilegSaved+"</p>");
        }

        List<Privileg> pList = (List<Privileg>) request.getAttribute(constants.privilegList);
        List<GroupPrivileg> gpList = (List<GroupPrivileg>) request.getAttribute(constants.groupPrivilegList);

        out.println(this.getFormBeginn(null));
        out.println(this.getHiddenAsAttribute());
        out.println(printHiddenField(constants.SECURITY_PROVIDER_PRIVILEG_MANAGEMENT, "true"));
        out.println("<table class=\"contenttable\">");
        out.println("<tr>");
        out.println("<th class=\"contentth\">"+privileg+"</td>");
        out.println("<th class=\"contentth\">"+hasPrivileg+"</td>");
        out.println("</tr>");
        for(int i=0;i<pList.size();i++) {
            Privileg p = pList.get(i);
            boolean checked = false;
            for(int j=0;j<gpList.size();j++) {
                if(gpList.get(j).getPrivilegId()==p.getId().intValue()) {
                    checked = true;
                    j = gpList.size();
                }

            }
            out.println("<tr>");
            out.println("<td class=\"contenttd\">"+p.getName()+"</td>");
            out.println("<td class=\"contenttd\"><input type=\"checkbox\" name=\""+constants.privilegId+p.getId().intValue()+"\""+(checked ? " checked" : "")+"></td>");
            out.println("</tr>");
        }
        out.println("</table>");
        out.println("<div><input type=\"submit\" name=\""+constants.privilegSave+"\" value=\""+save+"\" class=\"button\"/></div>");
        out.println(this.getFormEnd());
        
        out.println(this.getFormBeginn(null));
        out.println(this.getHiddenAsAttribute());
        out.println("<p><input type=\"submit\" name=\""+constants.privilegBack+"\" value=\""+back+"\" class=\"button\"/></p>");
        out.println(this.getFormEnd());
        
        out.println("</div>");
    }
}